package qldc.pm.tienan.searchcontainer;

import java.text.SimpleDateFormat;

import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;

import qldc.pm.tienan.constants.TienAnConstants;
import qldc.pm.tienan.searchcontainer.util.yeucautrichluc.YeuCauTrichLucSearchUtil;

import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.PortalPreferences;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

public class YeuCauTrichLucSearch extends SearchContainer<YeuCauTrichLucSearch> {
	private static Log _log = LogFactoryUtil.getLog(YeuCauTrichLucSearch.class);

	public static final String EMPTY_RESULTS_MESSAGE = "khong-tim-thay-yeu-cau-trich-luc-nao";

	public YeuCauTrichLucSearch(RenderRequest renderRequest, PortletURL iteratorURL) {
		super(renderRequest, new YeuCauTrichLucDisplayTerms(renderRequest),
				new YeuCauTrichLucSearchTerms(renderRequest), DEFAULT_CUR_PARAM,
				DEFAULT_DELTA, iteratorURL, null, EMPTY_RESULTS_MESSAGE);

		YeuCauTrichLucDisplayTerms displayTerms = (YeuCauTrichLucDisplayTerms) getDisplayTerms();
		SimpleDateFormat format = new SimpleDateFormat(TienAnConstants.DATE_PATTERN);

		iteratorURL.setParameter(YeuCauTrichLucSearchTerms.HO_VA_TEN_NGUOI_YEU_CAU_TRICH_LUC , displayTerms.getHoVaTenNguoiYeuCauTrichLuc());
		iteratorURL.setParameter(YeuCauTrichLucSearchTerms.CO_QUAN_NGUOI_YEU_CAU_TRICH_LUC , displayTerms.getCoQuanNguoiYeuCauTrichLuc());
		iteratorURL.setParameter(YeuCauTrichLucSearchTerms.SO_CMND_DOI_TUONG_TRICH_LUC, displayTerms.getSoCMNDDoiTuongTrichLuc());
		iteratorURL.setParameter(YeuCauTrichLucSearchTerms.HO_VA_TEN_DOI_TUONG_TRICH_LUC , displayTerms.getHoVaTenDoiTuongTrichLuc());
		iteratorURL.setParameter(YeuCauTrichLucSearchTerms.SO_DINH_DANH_CA_NHAN_DOI_TUONG_TRICH_LUC , displayTerms.getSoDinhDanhCaNhanDoiTuongTrichLuc());
		iteratorURL.setParameter(YeuCauTrichLucSearchTerms.SO_SO_HO_KHAU_DOI_TUONG_TRICH_LUC , displayTerms.getSoSoHoKhauDoiTuongTrichLuc());
		iteratorURL.setParameter(YeuCauTrichLucSearchTerms.TRANG_THAI_TRICH_LUC , StringUtil.valueOf(displayTerms.getTrangThaiTrichLuc()));
		
		iteratorURL.setParameter(YeuCauTrichLucSearchTerms.NGUOI_TAO_TRICH_LUC , displayTerms.getNguoiTaoTrichLuc());
		iteratorURL.setParameter(YeuCauTrichLucSearchTerms.TINH_ID , StringUtil.valueOf(displayTerms.getTinhId()));
		iteratorURL.setParameter(YeuCauTrichLucSearchTerms.QUAN_HUYEN_ID , StringUtil.valueOf(displayTerms.getQuanHuyenId()));
		iteratorURL.setParameter(YeuCauTrichLucSearchTerms.PHUONG_XA_ID , StringUtil.valueOf(displayTerms.getPhuongXaId()));
	
		
		if (displayTerms.getTuNgayTaoTrichLuc() != null) {
			iteratorURL.setParameter(YeuCauTrichLucSearchTerms.TU_NGAY_TAO_TRICH_LUC,
					format.format(displayTerms.getTuNgayTaoTrichLuc()));
		}
		if (displayTerms.getDenNgayTaoTrichLuc() != null) {
			iteratorURL.setParameter(NguoiCoTienAnDisplayTerms.NGAY_TOA_AN,
					format.format(displayTerms.getDenNgayTaoTrichLuc()));
		}
		try {
			PortalPreferences preferences = PortletPreferencesFactoryUtil.getPortalPreferences(renderRequest);

			String orderByCol = ParamUtil.getString(renderRequest, "orderByCol");
			String orderByType = ParamUtil.getString(renderRequest,"orderByType");
			String portletName = "tienan";

			if(Validator.isNotNull(orderByCol) && Validator.isNotNull(orderByType)){
				preferences.setValue(portletName, "order-by-col", orderByCol);
				preferences.setValue(portletName, "order-by-type", orderByType);

			} else{
				orderByCol = preferences.getValue(portletName, "order-by-col", "");
				orderByType = preferences.getValue(portletName, "order-by-type", "asc");
			}

			OrderByComparator orderByComparator = YeuCauTrichLucSearchUtil.getYeuCauTrichLucOrderByComparator(orderByCol, orderByType);

			setOrderByCol(orderByCol);
			setOrderByType(orderByType);
			setOrderByComparator(orderByComparator);
		} catch (SystemException e) {
			_log.warn(e);
		}
	}
}
